using System;
using System.Collections.Generic;
using System.Text;
using System.Data.SqlClient;
using System.Data;

using HHo_Model;

namespace HHo_Negocio
{
    public class GestorUpLoad
    {
        public bool EliminaFile(E_FileUpLoad eFile)
        {
            SqlParameter oP1, oP2, oP3;
            bool returnValue = false;

            oP1 = new SqlParameter("@INFTRA_ID", SqlDbType.Int);
            oP1.Direction = ParameterDirection.Input;
            oP1.Value = eFile.IdTrab;

            oP2 = new SqlParameter("@INFTRADET_SEQ", SqlDbType.SmallInt);
            oP2.Direction = ParameterDirection.Input;
            oP2.Value = eFile.SeqInfTrab;            

            oP3 = new SqlParameter("@FILE_UP_SEQ", SqlDbType.TinyInt);
            oP3.Direction = ParameterDirection.Input;
            oP3.Value = eFile.SeqFileUp;

            try
            {
                if(SQLHelper.ExecuteNonQuery(SQLHelper.ObtieneCadenaConexion(), CommandType.StoredProcedure, "[HHSE_FILEUP]", oP1, oP2, oP3) > 0)
                    returnValue = true;

                return returnValue;
            }
            catch
            {
                throw;
            }
        }


        public short InsertFile(E_FileUpLoad eFile)
        {
            SqlParameter oP1, oP2, oP3, oP4, oP5;

            oP1 = new SqlParameter("@INFTRA_ID", SqlDbType.Int);
            oP1.Direction = ParameterDirection.Input;
            oP1.Value = eFile.IdTrab;

            oP2 = new SqlParameter("@INFTRADET_SEQ", SqlDbType.SmallInt);
            oP2.Direction = ParameterDirection.Input;
            oP2.Value = eFile.SeqInfTrab;

            oP3 = new SqlParameter("@FILE_NOMB", SqlDbType.VarChar);
            oP3.Direction = ParameterDirection.Input;
            oP3.Value = eFile.NombFileUp;

            oP4 = new SqlParameter("@FILE_SIZE", SqlDbType.VarChar);
            oP4.Direction = ParameterDirection.Input;
            oP4.Value = eFile.SizeFileUp;

            oP5 = new SqlParameter("@FILE_UP_SEQ", SqlDbType.TinyInt);
            oP5.Direction = ParameterDirection.Output;

            SQLHelper.ExecuteNonQuery(SQLHelper.ObtieneCadenaConexion(), CommandType.StoredProcedure, "[HHSI_FILEUP]", oP1, oP2, oP3, oP4, oP5);

            return Int16.Parse(oP5.Value.ToString());

        }

        public List<E_FileUpLoad> ListarFilesUpload(E_FileUpLoad eFile)
        {
            SqlParameter oP1, oP2;
            SqlDataReader oRdr;
            E_FileUpLoad oFile;
            List<E_FileUpLoad> lst;

            oP1 = new SqlParameter("@INFTRA_ID", SqlDbType.Int);
            oP1.Direction = ParameterDirection.Input;
            oP1.Value = eFile.IdTrab;

            oP2 = new SqlParameter("@INFTRADET_SEQ", SqlDbType.SmallInt);
            oP2.Direction = ParameterDirection.Input;
            oP2.Value = eFile.SeqInfTrab;

            oRdr = SQLHelper.ExecuteReader(SQLHelper.ObtieneCadenaConexion(), CommandType.StoredProcedure, "[HHSS_FILEUP]", oP1, oP2);

            lst = new List<E_FileUpLoad>();

            if (oRdr != null)
            {
                using (oRdr)
                {
                    while (oRdr.Read())
                    {
                        oFile = new E_FileUpLoad();

                        if (oRdr.GetValue(0) != DBNull.Value)
                            oFile.IdTrab = oRdr.GetInt32(0);

                        if (oRdr.GetValue(1) != DBNull.Value)
                            oFile.SeqInfTrab = oRdr.GetInt16(1);

                        if (oRdr.GetValue(2) != DBNull.Value)
                            oFile.SeqFileUp = Int16.Parse(oRdr.GetValue(2).ToString());

                        if (oRdr.GetValue(3) != DBNull.Value)
                            oFile.NombFileUp = oRdr.GetString(3);

                        if (oRdr.GetValue(4) != DBNull.Value)
                            oFile.SizeFileUp = oRdr.GetString(4);

                        if (oRdr.GetValue(5) != DBNull.Value)
                            oFile.EstadoFileUp = oRdr.GetString(5);

                        lst.Add(oFile);
                        oFile = null;
                    }
                }
            }
            return lst;
        }
    }
}
